﻿using System;
using System.Linq;
using System.Text;
using Biz.EF.Gateway.Total;
using Biz.EF.Platform.Gxt;
using Biz.Properties;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class ProcessorMonthPlan4Total
    {
        private static ILog Log = LogManager.GetLogger(typeof (ProcessorMonthPlan4Total));
        public void GenMonthPlan4Total(object state)
        {
            while (true)
            {
                var now = DateTime.Now;
                if (now.Day == 1)
                {
                    try
                    {
                        //生成月计划指令下发
                        using (var gCtx = new GatewayTotalContext())
                        using (var pCtx = new PlatformGxtContext())
                        {
                            //try
                            //{
                            var totalMns = MonitorAndTotalMnMap.GetTotalMns();
                            if (totalMns == null) continue;
                            foreach (var mn in totalMns)
                            {
                                var company = InfoBuffer.GetCompanyByMn(mn, MnType.Total);
                                if (company == null) continue;
                                StringBuilder sb = new StringBuilder();
                                var qn = now.ToString("yyyyMMddHHmmssfff");
                                sb.Append("QN=" + qn + ";ST=32;CN=3093;PW=123456;MN=" + mn + ";Flag=1;CP=&&Month=" + now.ToString("yyyyMM") + ";");

                                var reals = pCtx.total_realtime_data.Where(t => t.ps_id_ == company.PsID).ToList();
                                foreach (var real in reals)
                                {
                                    if (real.month_allow_amount_ != null)
                                        sb.Append(real.pollutant_type_ + "-MonAll=" +
                                                  real.month_allow_amount_.Value.ToString("0.00"));
                                    else
                                    {
                                        sb.Append(real.pollutant_type_ + "-MonAll=1");
                                    }
                                    sb.Append(";");
                                }
                                sb.Append("&&");
                                if (Settings.Default.ToGatewayTotal)
                                {
                                    send_data_t data = new send_data_t
                                    {
                                        send_time = now,
                                        cn = "3093",
                                        is_sent = false,
                                        mn = mn,
                                        qn = qn,
                                        send_string = Crc.WrapByCrc(sb.ToString(),"total"),
                                        sent_time = now
                                    };
                                    gCtx.send_data_t.Add(data);
                                    gCtx.SaveChanges();
                                }
                                if (Settings.Default.ToPlatformGxt)
                                {
                                    var data =
                                        pCtx.gateway_sent_data
                                            .Where(t => t.CN.Equals("3093"))
                                            .Where(t => t.MN.Equals(mn)).FirstOrDefault(t => t.QN.Equals(qn));
                                    if (data == null)
                                    {
                                        data = new gateway_sent_data
                                        {
                                            IsProcessed = false,
                                            IsValid = true,
                                            MN = mn,
                                            ST = "32",
                                            CN = "3093",
                                            QN = qn,
                                            SentString = Crc.WrapByCrc(sb.ToString(),"total")
                                        };
                                        pCtx.gateway_sent_data.Add(data);
                                        pCtx.SaveChanges();
                                    }
                                }
                            }
                            //}
                            //catch (Exception innerEx)
                            //{
                            //    LogHelper.Error(typeof(BizProcessor), innerEx);
                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error( ex);
                    }
                    finally
                    {
                        ThreadUtil.Sleep(25 * 60 * 60 * 1000);
                    }
                }
                ThreadUtil.Sleep(30 * 60 * 1000);
            }
            // ReSharper disable once FunctionNeverReturns
        }

    }
}
